package cn.zhoutingrui.pethome.system.permission.controller;

import cn.zhoutingrui.pethome.system.permission.service.IPermissionService;
import cn.zhoutingrui.pethome.system.permission.domain.Permission;
import cn.zhoutingrui.pethome.system.permission.query.PermissionQuery;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;


    /**
     * 保存和修改公用的
     * @param permission  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody Permission permission){
        try {
            if( permission.getId()!=null)
                permissionService.update(permission);
            else
                permissionService.add(permission);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            permissionService.delete(id);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Permission get(@PathVariable("id")Long id)
    {
        return permissionService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Permission> list(){
        return permissionService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResultUtil json(@RequestBody PermissionQuery query)
    {
        try {
            List<Permission> list = permissionService.findQuery(query);
            Long count = permissionService.findByQueryCount(query);
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(list).setTotal(count);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail();
        }
    }
}
